package com.suny.juc;

import java.util.concurrent.CountDownLatch;

/**
 * 阻塞线程 当指定数量的线程到达后开始往下运行
 * @author sunny
 * @date 2019/3/6
 */
public class CountDownLatchTest {
    private static CountDownLatch countDownLatch = new CountDownLatch(5);

    /**
     * Boss线程
     *
     * @author sunny
     * @date 2019/3/6
     */
    static class BossThread extends Thread {
        @Override
        public void run() {
            System.out.println("Boss在会议室等待，总共有" + countDownLatch.getCount() + "个人开会...");
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("所有人都已经到齐了，开会吧...");
        }
    }

    /**
     *
     */
    static class EmployeeThread extends Thread {
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "，到达会议室....");
            //线程到达  count - 1
            countDownLatch.countDown();
        }
    }

    public static void main(String[] args) {
        new BossThread().start();

        for (long i = 0, j = countDownLatch.getCount(); i < j; i++) {
            new EmployeeThread().start();
        }
    }

}



